package com.zh.work.controller.platform.channel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.dao.platform.channel.ChannelQrcodeMapper;
import com.zh.work.entity.channel.ChannelQrCodeEntity;
import com.zh.work.entity.channel.QrCodeRateEntity;
import com.zh.work.service.platform.channel.QrcodeChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 二维码渠道管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/04
 * @Version: 1.0.1
 */
@Api(value = "二维码渠道管理控制器", produces = "application/json", tags = {"二维码渠道管理控制器"})
@RestController
@RequestMapping("/channelQrCode")
public class QrcodeChannelController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/04
     */
    @Autowired
    private ChannelQrcodeMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/04
     */
    @Autowired
    private QrcodeChannelService service;



    @ApiOperation(value = "新增一条记录", tags = {"putAddChannelQrCode"}, notes = "新增一条记录")
    @PutMapping("/putAddChannelQrCode")
    @ResponseBody
    public JSONResult<Object> putAddChannelQrCode(@ApiParam(value = "二维码渠道实体类", name = "ChannelQrCodeEntity")@RequestBody ChannelQrCodeEntity channelQrcodeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = this.service.putChannelQrCode(channelQrcodeEntity);
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                switch (i){
                    case -1:
                        jsonResult.setMessage("请选择一个机构再添加!");
                        return jsonResult;
                    case -2:
                        jsonResult.setMessage("费率成本解析错误,请填写完整费率信息!");
                        return jsonResult;
                    case -3:
                        jsonResult.setMessage("添加费率信息出错!");
                        return jsonResult;
                    case -4:
                        jsonResult.setMessage("添加二维码渠道信息出错!");
                        return jsonResult;
                }
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModChannelQrCodeById"}, notes = "通过id修改信息")
    @PutMapping("/putModChannelQrCodeById")
    @ResponseBody
    public JSONResult<Object> putModChannelQrCodeById(@ApiParam(value = "二维码渠道实体类", name = "ChannelQrCodeEntity")@RequestBody
                                                                  ChannelQrCodeEntity channelQrcodeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = this.service.modChannelQrCode(channelQrcodeEntity);
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                switch (i){
                    case -1:
                        jsonResult.setMessage("请选择一个机构再添加!");
                        return jsonResult;
                    case -2:
                        jsonResult.setMessage("费率成本解析错误,请填写完整费率信息!");
                        return jsonResult;
                    case -3:
                        jsonResult.setMessage("添加费率信息出错!");
                        return jsonResult;
                    case -4:
                        jsonResult.setMessage("添加二维码渠道信息出错!");
                        return jsonResult;
                }
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteChannelQrCodeById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteChannelQrCodeById")
    public JSONResult<Object> deleteChannelQrCodeById(@ApiParam(value = "二维码渠道实体类", name = "ChannelQrCodeEntity") ChannelQrCodeEntity channelQrcodeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (channelQrcodeEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteChannelQrCodeBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteChannelQrCodeBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteChannelQrCodeBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

//    @PreAuthorize("hasAuthority('/basic_auth')")
    @ApiOperation(value = "根据id查询详细信息", tags = {"getChannelQrcodeById"}, notes = "根据id查询详细信息")
    @GetMapping("/getChannelQrcodeById")
    public JSONResult<ChannelQrCodeEntity> getChannelQrcodeById(@ApiParam(value = "二维码渠道实体类", name = "ChannelQrCodeEntity") ChannelQrCodeEntity channelQrcodeEntity) {
        JSONResult<ChannelQrCodeEntity> jsonResult = new JSONResult<>();
        try {
            ChannelQrCodeEntity result = channelQrcodeEntity.selectById(channelQrcodeEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesChannelQrCodes"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesChannelQrCodes")
    public JSONResult<List<ChannelQrCodeEntity>> getPagesChannelQrCodes(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize")Integer pageSize,
            @ApiParam(value = "二维码渠道实体类", name = "qrCodeEntity") ChannelQrCodeEntity qrCodeEntity) {
        JSONResult<List<ChannelQrCodeEntity>> jsonResult = new JSONResult<>();
        try {
            System.out.println(qrCodeEntity.getChannelCode());
            IPage<ChannelQrCodeEntity> result = this.mapper.selectPage(
                    new Page<ChannelQrCodeEntity>(pageNo, pageSize),
                    new LambdaQueryWrapper<ChannelQrCodeEntity>()
                    .like(StringUtils.isNotEmpty(qrCodeEntity.getChannelCode()), ChannelQrCodeEntity::getChannelCode, qrCodeEntity.getChannelCode())
                    .like(StringUtils.isNotEmpty(qrCodeEntity.getChannelName()), ChannelQrCodeEntity::getChannelName, qrCodeEntity.getChannelName())
            );
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "获取指定渠道编号的费率", tags = {"getRateByChannelCode"}, notes = "获取指定渠道编号的费率")
    @GetMapping("getRateByChannelCode")
    public JSONResult<List<QrCodeRateEntity>> getRateByChannelCode(@ApiParam(value="渠道编号",name = "channelCode")@RequestParam("channelCode")String channelCode) {
        JSONResult<List<QrCodeRateEntity>> jsonResult = new JSONResult<>();
        List<QrCodeRateEntity> list = this.mapper.getRateByChannelCode(channelCode);
        if (list.size() > 0) {
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
        } else {
            jsonResult.setMessage("渠道代码【" + channelCode + "】的费率为空.");
            jsonResult.setSuccess(false);
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改渠道二维码状态", tags = {"putModChannelQrCodeStatus"}, notes = "修改渠道二维码状态")
    @PutMapping("putModChannelQrCodeStatus")
    @ResponseBody
    public JSONResult<Object> putModChannelQrCodeStatus( @ApiParam(value = "二维码渠道实体类", name = "qrCodeEntity")@RequestBody ChannelQrCodeEntity qrCodeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<Object>();
        try {
            int i = this.service.putAuditChannelQrCode(qrCodeEntity.getStatus(), Integer.valueOf(qrCodeEntity.getId().toString()));
            if (i > 0 )
                jsonResult.setSuccess(true);
            else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请检查状态设置是否正确!");
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询二维码渠道信息", tags = {"getChannelQrCode"}, notes = "查询二维码渠道信息")
    @GetMapping("getChannelQrCode")
    public JSONResult< List<ChannelQrCodeEntity>> getChannelQrCode() {
        JSONResult< List<ChannelQrCodeEntity>> jsonResult = new JSONResult<>();
        try {
            List<ChannelQrCodeEntity> list = new ArrayList<>();
            list = this.service.getChannelQrCode();
            jsonResult.setSuccess(true);
            jsonResult.setData(list);
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }
}
